home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Database How-To / Visual Basic 4 Database - How-to (The Waite Group)(1995).iso / creattbl.fr_ / creattbl.fr
Text File  |  1995-03-09  |  10KB  |  309 lines

  1. VERSION 4.00
  2. Begin VB.Form Form1 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Table Creator"
  5.    ClientHeight    =   2880
  6.    ClientLeft      =   645
  7.    ClientTop       =   1455
  8.    ClientWidth     =   7230
  9.    BeginProperty Font 
  10.       name            =   "MS Sans Serif"
  11.       charset         =   1
  12.       weight          =   700
  13.       size            =   8.25
  14.       underline       =   0   'False
  15.       italic          =   0   'False
  16.       strikethrough   =   0   'False
  17.    EndProperty
  18.    Height          =   3285
  19.    Left            =   585
  20.    LinkTopic       =   "Form1"
  21.    ScaleHeight     =   2880
  22.    ScaleWidth      =   7230
  23.    Top             =   1110
  24.    Width           =   7350
  25.    Begin VB.ListBox lstTables 
  26.       Height          =   1980
  27.       Left            =   180
  28.       TabIndex        =   4
  29.       Top             =   660
  30.       Width           =   1695
  31.    End
  32.    Begin VB.CommandButton cmdCreateTable 
  33.       Caption         =   "--> Create &Table -->"
  34.       Enabled         =   0   'False
  35.       Height          =   1035
  36.       Left            =   2100
  37.       TabIndex        =   2
  38.       Top             =   900
  39.       Width           =   2055
  40.    End
  41.    Begin VB.CommandButton cmdClose 
  42.       Cancel          =   -1  'True
  43.       Caption         =   "Cl&ose"
  44.       Height          =   495
  45.       Left            =   2100
  46.       TabIndex        =   1
  47.       Top             =   2160
  48.       Width           =   2055
  49.    End
  50.    Begin VB.CommandButton cmdCreateDatabase 
  51.       Caption         =   "&Create &Database"
  52.       Height          =   495
  53.       Left            =   2100
  54.       TabIndex        =   0
  55.       Top             =   180
  56.       Width           =   2055
  57.    End
  58.    Begin VB.Label Label2 
  59.       AutoSize        =   -1  'True
  60.       BackColor       =   &H00C0C0C0&
  61.       Caption         =   "Created Tables and Fields:"
  62.       Height          =   195
  63.       Left            =   4380
  64.       TabIndex        =   6
  65.       Top             =   360
  66.       Width           =   2295
  67.    End
  68.    Begin VB.Label Label1 
  69.       AutoSize        =   -1  'True
  70.       BackColor       =   &H00C0C0C0&
  71.       Caption         =   "Available Tables:"
  72.       Height          =   195
  73.       Left            =   180
  74.       TabIndex        =   5
  75.       Top             =   360
  76.       Width           =   1485
  77.    End
  78.    Begin MSOutl.Outline outTablesAndFields 
  79.       Height          =   1995
  80.       Left            =   4380
  81.       TabIndex        =   3
  82.       Top             =   660
  83.       Width           =   2595
  84.       _version        =   65536
  85.       _extentx        =   4577
  86.       _extenty        =   3519
  87.       _stockprops     =   77
  88.       backcolor       =   16777215
  89.       pictureplus     =   "CREATTBL.frx":0000
  90.       pictureminus    =   "CREATTBL.frx":0172
  91.       pictureleaf     =   "CREATTBL.frx":02E4
  92.       pictureopen     =   "CREATTBL.frx":0456
  93.       pictureclosed   =   "CREATTBL.frx":05C8
  94.    End
  95.    Begin MSComDlg.CommonDialog CommonDialog1 
  96.       Left            =   1620
  97.       Top             =   60
  98.       _version        =   65536
  99.       _extentx        =   847
  100.       _extenty        =   847
  101.       _stockprops     =   0
  102.       cancelerror     =   -1  'True
  103.       defaultext      =   "MDB"
  104.       dialogtitle     =   "Create New Database"
  105.       filter          =   "Microsoft Acccess (*.MDB)|*.MDB"
  106.       flags           =   5000
  107.    End
  108. End
  109. Attribute VB_Name = "Form1"
  110. Attribute VB_Creatable = False
  111. Attribute VB_Exposed = False
  112. Option Explicit
  113.  
  114. ' Declare the text field lengths as constants
  115. Private Const LEN_Customer_Name = 40
  116. Private Const LEN_Street_Address = 80
  117. Private Const LEN_City = 25
  118. Private Const LEN_State = 2
  119. Private Const LEN_Zip_Code = 10
  120. Private Const LEN_Country = 25
  121. Private Const LEN_Item_Number = 16
  122. Private Const LEN_Item_Description = 100
  123.  
  124. ' Declare the database at form level.
  125. Dim db As Database
  126.  
  127. Private Sub cmdCreateDatabase_Click()
  128.     Dim fn As String
  129.     Dim tblDef As TableDef
  130.     
  131.     On Error GoTo CreateError
  132.     
  133.     ' Set the filename to a null string and display the common dialog box.
  134.     CommonDialog1.FileName = ""
  135.     CommonDialog1.ShowSave
  136.  
  137.     ' The user entered a filename for the new database. Assign it to the variable fn.
  138.     Screen.MousePointer = 11
  139.     fn = CommonDialog1.FileName
  140.  
  141.     ' Create the new database file.
  142.     Set db = DBEngine.Workspaces(0).CreateDatabase(fn, dbLangGeneral)
  143.     Screen.MousePointer = 0
  144.  
  145.     ' Verify that the file now exists on disk.
  146.     If Dir(fn) = CommonDialog1.FileTitle Then
  147.     
  148.         ' The file exists, so display a message.
  149.         Form1.Caption = "Table Creator for " & UCase$(fn)
  150.         
  151.         ' Clear the existing list and outline
  152.         lstTables.Clear
  153.         outTablesAndFields.Clear
  154.         
  155.         ' Fill the list box with the sample tables
  156.         lstTables.AddItem "Customers"
  157.         lstTables.AddItem "Items"
  158.         lstTables.AddItem "Order Items"
  159.         lstTables.AddItem "Orders"
  160.         
  161.         ' Enable the Create Table features.
  162.         cmdCreateTable.Enabled = True
  163.     Else
  164.         MsgBox "Could not create " & fn, vbExclamation
  165.     End If
  166. Exit Sub
  167.  
  168. CreateError:
  169.     Screen.MousePointer = 0
  170.     If Err.Number = 32755 Then
  171.         ' The user cancelled the dialog box, so do nothing.
  172.     Else
  173.         ' Some other error, so show the user the description.
  174.         MsgBox Err.Description
  175.     End If
  176. Exit Sub
  177. End Sub
  178.  
  179. Private Sub cmdCreateTable_Click()
  180.     Dim tableName As String
  181.     Dim tblDef As TableDef
  182.     
  183.     On Error GoTo TableCreateError
  184.     
  185.     If lstTables.ListIndex > -1 Then
  186.     
  187.         ' The user has a table selected, so create a new table definition
  188.         ' in the database with the name of the table.
  189.         Screen.MousePointer = 11
  190.         Set tblDef = db.CreateTableDef(lstTables.Text)
  191.         
  192.         ' Now add the appropriate fields to the table.
  193.         AddFields tblDef
  194.        
  195.         
  196.         ' With all the fields in place, append the table defintion to the database.
  197.         db.TableDefs.Append tblDef
  198.         
  199.         ' Take the list off the list of available tables.
  200.         tableName = lstTables.Text
  201.         RemoveFromList tableName
  202.         
  203.         ' Put the table and its fields into the outline of tables in the database.
  204.         AddToOutline tableName
  205.     End If
  206.     Screen.MousePointer = 0
  207.     
  208. Exit Sub
  209.  
  210. TableCreateError:
  211.     Screen.MousePointer = 0
  212.     MsgBox Err.Description
  213. Exit Sub
  214.  
  215. End Sub
  216. Sub AddFields(tblDef As TableDef)
  217.  
  218.     Dim fldDef As Field
  219.     
  220.     ' For each field, first create the field TableDef
  221.     ' Then add it to the field list for the table
  222.     Select Case lstTables.Text
  223.         Case "Customers"
  224.             Set fldDef = tblDef.CreateField("Customer Number", dbLong)
  225.             tblDef.Fields.Append fldDef
  226.             Set fldDef = tblDef.CreateField("Customer Name", dbText, LEN_Customer_Name)
  227.             tblDef.Fields.Append fldDef
  228.             Set fldDef = tblDef.CreateField("Street Address", dbText, LEN_Street_Address)
  229.             tblDef.Fields.Append fldDef
  230.             Set fldDef = tblDef.CreateField("City", dbText, LEN_City)
  231.             tblDef.Fields.Append fldDef
  232.             Set fldDef = tblDef.CreateField("State", dbText, LEN_State)
  233.             tblDef.Fields.Append fldDef
  234.             Set fldDef = tblDef.CreateField("Zip Code", dbText, LEN_Zip_Code)
  235.             tblDef.Fields.Append fldDef
  236.             Set fldDef = tblDef.CreateField("Country", dbText, LEN_Country)
  237.             tblDef.Fields.Append fldDef
  238.         Case "Items"
  239.             Set fldDef = tblDef.CreateField("Item Number", dbText, LEN_Item_Number)
  240.             tblDef.Fields.Append fldDef
  241.             Set fldDef = tblDef.CreateField("Item Description", dbText, LEN_Item_Description)
  242.             tblDef.Fields.Append fldDef
  243.             Set fldDef = tblDef.CreateField("Price Each", dbCurrency)
  244.             tblDef.Fields.Append fldDef
  245.         Case "Orders"
  246.             Set fldDef = tblDef.CreateField("Customer Number", dbLong)
  247.             tblDef.Fields.Append fldDef
  248.             Set fldDef = tblDef.CreateField("Order Number", dbLong)
  249.             tblDef.Fields.Append fldDef
  250.             Set fldDef = tblDef.CreateField("Order Date", dbDate)
  251.             tblDef.Fields.Append fldDef
  252.             Set fldDef = tblDef.CreateField("Ship Date", dbDate)
  253.             tblDef.Fields.Append fldDef
  254.             Set fldDef = tblDef.CreateField("Tax", dbCurrency)
  255.             tblDef.Fields.Append fldDef
  256.             Set fldDef = tblDef.CreateField("Shipping Charge", dbCurrency)
  257.             tblDef.Fields.Append fldDef
  258.         Case "Order Items"
  259.             Set fldDef = tblDef.CreateField("Order Number", dbLong)
  260.             tblDef.Fields.Append fldDef
  261.             Set fldDef = tblDef.CreateField("Item Number", dbText, LEN_Item_Number)
  262.             tblDef.Fields.Append fldDef
  263.             Set fldDef = tblDef.CreateField("Quantity", dbLong)
  264.             tblDef.Fields.Append fldDef
  265.     End Select
  266.  
  267. End Sub
  268.  
  269. Private Sub lstTables_DblClick()
  270.     cmdCreateTable_Click
  271. End Sub
  272. Sub RemoveFromList(tableName As String)
  273.     Dim i As Integer
  274.     
  275.     ' Find the table passed as the argument in the list and remove it from the list.
  276.     For i = 0 To lstTables.ListCount - 1
  277.         If lstTables.List(i) = tableName Then
  278.             lstTables.RemoveItem i
  279.             Exit For
  280.         End If
  281.     Next i
  282.     
  283. End Sub
  284. Sub AddToOutline(tableName As String)
  285.     Dim tableIndex As Integer
  286.     Dim tblDef As TableDef
  287.     Dim i As Integer
  288.  
  289.     ' Indicate that the table name is to be added at the top level of the outline.
  290.     outTablesAndFields.ListIndex = -1
  291.     
  292.     ' Add the table to the outline.
  293.     outTablesAndFields.AddItem tableName
  294.     
  295.     ' Store the just-added table's ListIndex property in a variable.
  296.     tableIndex = outTablesAndFields.ListCount - 1
  297.     
  298.     ' Add each field in the table to the outline as a subitem of the table name.
  299.     Set tblDef = db.TableDefs(tableName)
  300.     For i = 0 To tblDef.Fields.Count - 1
  301.         outTablesAndFields.ListIndex = tableIndex
  302.         outTablesAndFields.AddItem tblDef.Fields(i).Name
  303.     Next
  304. End Sub
  305. Private Sub cmdClose_Click()
  306.     End
  307. End Sub
  308.  
  309.